package cn.edu.hunau.pojo;



import cn.edu.hunau.service.ProductService;
import cn.edu.hunau.service.impl.ProductServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述购物车
 * @Author 苏佳
 * @Date 2022 11 07 14 19
 **/
public class Car {
    /**
     * 总金额
     */
    private double total;
    private List<CarItem> items;

    public Car(){
        items = new ArrayList<>();
    }
    ArrayList<CarItem> newItems = new ArrayList<>();

    /**
     * 创建一个新的car由勾选的carItem形成
     * @param carItems
     * @return
     */
    public Car createNewCar(List<Map<String,Object>> carItems){
        ProductService productService = new ProductServiceImpl();
        Car newCar = new Car();
        double sum=0;
        for (int i = 0; i < carItems.size(); i++) {
            int pid = (int)carItems.get(i).get("pid");
            Product product = productService.findProductById(pid);
            int size = (int)carItems.get(i).get("size");
            int qty = (int)carItems.get(i).get("qty");
            double shoppingPrice = (int)carItems.get(i).get("shoppingPrice");
            CarItem item = new CarItem(product, qty,size,shoppingPrice);
            //判断该item是否存在
            if(items.contains(item)){
                newItems.add(item);
            }
            sum+=qty*shoppingPrice;
        }
        newCar.setItems(newItems);
        newCar.setTotal(sum);
        return newCar;
    }
    /**
     * 将商品添加到购物车中
     * @param product
     * @param qty
     */
    public void add(Product product,int qty,int size,double shoppingPrice){
        CarItem item = new CarItem(product, qty,size,shoppingPrice);
        //1. 如果商品在购物车中不存在, 直接添加
        if(items.contains(item)){   //内部调用CarItem的equals方法进行比较
            //商品已经存在 且size相同, 实现qty的累加
            int i = items.indexOf(item);
            if(items.get(i).getSize()==item.getSize()){
                int qtyNum = items.get(i).getQty();
                items.get(i).setQty(qtyNum+qty);
            }else {
                items.add(item);
            }

        }else{
            items.add(item);
        }
    }

    /**
     * 修改数量
     * @param pid
     * @param qty
     */
    public void update(int pid,int qty){
        for (int i = 0; i < items.size(); i++) {
            CarItem item = items.get(i);
            if(item.getProduct().getId()==pid){
                item.setQty(qty);
            }
        }
    }

    /**
     * 删除条目
     *
     */
    public void remove(int pid,int size){
        for (int i = 0; i < items.size(); i++) {
            CarItem item = items.get(i);
            if(item.getProduct().getId()==pid && item.getSize()==size){
                items.remove(i);
            }
        }
    }

    /**
     * 清空购物车
     */
    public void clear(){
        items.clear();
    }

    public double getTotal() {
        total=0;
        for (int i = 0; i < items.size(); i++) {
            CarItem item = items.get(i);
            total += item.getShoppingPrice() * item.getQty();
        }
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public List<CarItem> getItems() {
        return items;
    }

    public void setItems(List<CarItem> items) {
        this.items = items;
    }
}
